package com.igd.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.igd.common.constant.RequestConstant;
import com.igd.pojo.vo.ExtData;
import com.igd.pojo.vo.PageList;
import com.igd.utils.JsonUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @createDate 2013-6-20
 *
 * @author QIAOYU
 *
 * @version 1.0
 *
 * @description 公用Action继承类
 */

public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = -9069129394695516189L;
	
	private final String UTF_8 = "UTF-8";
	
	protected final String JSON_PAGE = "jsonPage";
	
	/**
	 * 分页数据对象
	 */
	private PageList page = new PageList();

	/**
	 * 前台传入的当前dom对象Id 
	 */
	private String _;
	
	/**
	 * 解析请求的参数,获取所有请求参数的名称然后封装成集合对象返回
	 * 
	 * @param ignore 忽略解析的参数名字符串数组,如不忽略数据可传入null
	 * 
	 * @return Map<String,String>
	 */
	protected Map<String,String> pasrseReqParam(String [] ignore){
		
		@SuppressWarnings("unchecked")
		Enumeration<String> paramNames = this.getReuquest().getParameterNames();
		
		if(null != paramNames){
			
			Map<String,String> params = new HashMap<String,String>();
			
			while(paramNames.hasMoreElements()){
				
				String paramName = (String) paramNames.nextElement();
				
				if(StringUtils.isNotEmpty(paramName) && !this.checkIgnore(ignore,paramName)){
					
					String paramKey = paramName;
					
					if(paramName.indexOf(".") > -1){
						
						paramKey = paramName.substring(paramName.indexOf(".") + 1,paramName.length());
						
					}
					
					if(StringUtils.isNotEmpty(paramKey)){
						
						String paramVal = String.valueOf(this.getRequestValue(paramName));
						
						if(StringUtils.isNotEmpty(paramVal)){
							
							params.put(paramKey,paramVal);

						}
					}
				}
			}
			
			return params;
		}
		
		return null;
	}
	
	/**
	 * 解析请求的参数,获取所有请求参数的名称然后封装成集合对象返回,此方法默认忽略分页的字段
	 * 
	 * @return
	 */
	protected Map<String,String> pasrseReqParam(){
		
		String [] ignore = new String[]{"page."};
		
		return this.pasrseReqParam(ignore);
		
	}
	
	/**
	 * 校验忽略集合是否有匹配,如果不忽略则数组可为null
	 * 
	 * @param ignore
	 * 
	 * @param value
	 * 
	 * @return boolean
	 */
	private boolean checkIgnore(String [] ignore,String value){
		
		if(null != ignore && ignore.length > 0){
			
			for(String val : ignore){
				
				if(val.startsWith(value)){
					
					return true;

				}
			}
		}
		
		return false;
	}
	
	/**
	 * 在ActionContext中设置值
	 * 
	 * @param key
	 * 
	 * @param value
	 */
	protected void put(String key,Object value){
		
		this.getActionContext().put(key, value);
	}
	
	/**
	 * 在ActionContext中设置JSON返回值,value为标准的JSON扩展对象
	 * 
	 * @param value
	 */
	protected void putExtData(ExtData<?> value){
		
		this.put(RequestConstant.EXT_DATA_KEY, value);
	}
	
	/**
	 * 在ActionContext中设置JSON返回值,此方法仅支持Ajax返回值为非JSON类型的请求
	 * 
	 * @param o
	 */
	protected void putExtData(Object o){
		
		this.put(RequestConstant.EXT_DATA_KEY, o);
	}
	
	/**
	 * 获取当前请求的ActionContext
	 * 
	 * @return
	 */
	protected ActionContext getActionContext(){
		
		return ServletActionContext.getActionContext(this.getReuquest());

	}
	
	/**
	 * 获取HttpServletRequest
	 * 
	 * @return HttpServletRequest
	 */
	protected HttpServletRequest getReuquest(){
		
		return ServletActionContext.getRequest();

	}
	
	/**
	 * 获取HttpServletResponse
	 * 
	 * @return HttpServletResponse
	 */
	protected HttpServletResponse getResponse(){
		
		return ServletActionContext.getResponse();
		
	}
	
	/**
	 * 获取HttpSession
	 * 
	 * @return
	 */
	protected HttpSession getSession(){
		
		return this.getReuquest().getSession();

	}
	
	/**
	 * 获取request中的值
	 * 
	 * @param paramName
	 * 
	 * @return
	 */
	protected Object getRequestValue(String paramName){
		
		return this.getReuquest().getParameter(paramName);

	}
	
	/**
	 * 设置request中的值
	 * 
	 * @param key
	 * 
	 * @param value
	 */
	protected void setRequestValue(String key,Object value){
		
		this.getReuquest().setAttribute(key, value);

	}
	
	/**
	 * response输出
	 * 
	 * @param content
	 * 
	 * @param contentType
	 * 
	 * @throws IOException
	 */
	protected void responseOut(String content,String contentType) throws IOException{
		
		HttpServletResponse response = this.getResponse();
		
		response.setCharacterEncoding(UTF_8);
		
		response.setContentType(contentType);
		
		PrintWriter out = response.getWriter();
		
		out.write(content);

	}
	
	/**
	 * 获取session中的值
	 * 
	 * @param key
	 * 
	 * @return
	 */
	protected Object getSessionValue(String key){
		
		return this.getSession().getAttribute(key);

	}
	
	/**
	 * 获取session中的值
	 * 
	 * @param key
	 * 
	 * @param o
	 */
	protected void setSessionValue(String key,Object o){
		
		this.getSession().setAttribute(key, o);

	}
	
	/**
	 * 根据传入对象产生Json字符串
	 * 
	 * @param o
	 * 
	 * @return
	 */
	protected String toJsonString(Object o){
		
		return JsonUtil.toJavaScriptJsonString(o);

	}
	
	/**
	 * 根据传入Json字符串反转成指定对象
	 * 
	 * @param jsonStr
	 * 
	 * @param clz
	 * 
	 * @return <T> T
	 */
	protected <T> T toBean(String jsonStr,Class<T> clz){
		
		return StringUtils.isNotEmpty(jsonStr) && null != clz ? JsonUtil.parseObject(jsonStr, clz) : null;

	}

	public String get_() {
		return _;
	}

	public void set_(String _) {
		this._ = _;
	}
	
	public PageList getPage() {
		return page;
	}

	public void setPage(PageList page) {
		this.page = page;
	}
}
